#include "Scene.h"
#include "Physics/Physics.h"

using namespace std;
using namespace core;

Scene::Scene( )
{
	_camera = 0;
	_map = 0;
}

Scene::Scene( Camera* camera, Map* map )
{
	_camera = camera;
	_map = map;
}

Scene::~Scene( )
{
}

void Scene::Update( float dt )
{
	physics::Physics::GetEngine( )->Update( dt );

	for( IteratorPtr<IEntity*> entities( _entities.GetIterator() );
			!entities->IsEnd( );
			entities->Next( ) )
	{
		entities->Current( )->Update( dt );
	}
	_map->Update( dt );
	_camera->Update( dt );
}

void Scene::Render( )
{
	if( _camera != 0 )
	{
		core::Renderer::SetCamera( _camera->GetPos( ), _camera->GetScale( ) );
		//Sound::SetListenerParameters( _camera->GetPos( ), _camera->GetScale( ) );
	}
	Renderer::BeginDraw( );

	for( IteratorPtr<IEntity*> entities( _entities.GetIterator() );
			!entities->IsEnd( );
			entities->Next( ) )
	{
		entities->Current( )->Draw( );
	}

	_map->Draw( );
	Renderer::EndDraw( );
}

bool Scene::IsActive( )
{
	return !Keyboard::IsKeyPressed( KeyboardKey::ESC ) && Window::IsOpened( );
}

void Scene::AddEntity( IEntity* entity )
{
	_entities.Add( entity );

}

void Scene::RemoveEntity( IEntity* entity )
{
	_entities.Remove( entity );
}

void Scene::AddPlayer( IPlayer* player )
{
	_players.Add( player );
}

void Scene::RemovePlayer( IPlayer* player )
{
	_players.Remove( player );
}


